<!doctype html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="LVS,keepalived," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.1" />






<meta name="description" content="&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;软件负载均衡一般通过两种方式来实现：基于操作系统的软负载实现和基于第三方应用的软负载实现。LVS就是基于Linux操作系统实现的一种软负载，HAProxy就是开源的并且基于第三应用实现的软负载。 &amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;HAProx">
<meta name="keywords" content="LVS,keepalived">
<meta property="og:type" content="article">
<meta property="og:title" content="haproxy+keepalived实现高可用负载均衡">
<meta property="og:url" content="https://hcldirgit.github.io/2017/09/03/LVS/20.  haproxy+keepalived实现高可用负载均衡/index.html">
<meta property="og:site_name" content="失落的乐章">
<meta property="og:description" content="&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;软件负载均衡一般通过两种方式来实现：基于操作系统的软负载实现和基于第三方应用的软负载实现。LVS就是基于Linux操作系统实现的一种软负载，HAProxy就是开源的并且基于第三应用实现的软负载。 &amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;HAProx">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/haproxy+keepalived%E5%AE%9E%E7%8E%B0%E9%AB%98%E5%8F%AF%E7%94%A8%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1/01.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/haproxy+keepalived%E5%AE%9E%E7%8E%B0%E9%AB%98%E5%8F%AF%E7%94%A8%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1/02.jpeg?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/haproxy+keepalived%E5%AE%9E%E7%8E%B0%E9%AB%98%E5%8F%AF%E7%94%A8%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1/03.jpeg?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/haproxy+keepalived%E5%AE%9E%E7%8E%B0%E9%AB%98%E5%8F%AF%E7%94%A8%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1/04.jpeg?raw=true">
<meta property="og:updated_time" content="2017-09-01T08:53:15.859Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="haproxy+keepalived实现高可用负载均衡">
<meta name="twitter:description" content="&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;软件负载均衡一般通过两种方式来实现：基于操作系统的软负载实现和基于第三方应用的软负载实现。LVS就是基于Linux操作系统实现的一种软负载，HAProxy就是开源的并且基于第三应用实现的软负载。 &amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;HAProx">
<meta name="twitter:image" content="https://github.com/hcldirgit/image/blob/master/haproxy+keepalived%E5%AE%9E%E7%8E%B0%E9%AB%98%E5%8F%AF%E7%94%A8%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1/01.png?raw=true">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://hcldirgit.github.io/2017/09/03/LVS/20.  haproxy+keepalived实现高可用负载均衡/"/>





  <title>haproxy+keepalived实现高可用负载均衡 | 失落的乐章</title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  




<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
  ga('create', '85*****1', 'auto');
  ga('send', 'pageview');
</script>


  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?87980c**************99ec5e26fb5";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>











  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">失落的乐章</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">技术面前，永远都是学生。</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-message">
          <a href="/message" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-external-link"></i> <br />
            
            留言
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://hcldirgit.github.io/2017/09/03/LVS/20.  haproxy+keepalived实现高可用负载均衡/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="失落的乐章">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/0.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="失落的乐章">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">haproxy+keepalived实现高可用负载均衡</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-09-03T02:04:46+08:00">
                2017-09-03
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;软件负载均衡一般通过两种方式来实现：基于操作系统的软负载实现和基于第三方应用的软负载实现。LVS就是基于Linux操作系统实现的一种软负载，HAProxy就是开源的并且基于第三应用实现的软负载。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;HAProxy相比LVS的使用要简单很多，功能方面也很丰富。当 前，HAProxy支持两种主要的代理模式:”tcp”也即4层（大多用于邮件服务器、内部协议通信服务器等），和7层（HTTP）。在4层模式 下，HAProxy仅在客户端和服务器之间转发双向流量。7层模式下，HAProxy会分析协议，并且能通过允许、拒绝、交换、增加、修改或者删除请求 (request)或者回应(response)里指定内容来控制协议，这种操作要基于特定规则。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;现在用HAProxy主要在于它有以下优点，这里总结下：</p>
<ul>
<li><p>免费开源，稳定性也是非常好，这个可通过我做的一些小项目可以看出来，单Haproxy也跑得不错，稳定性可以与LVS相媲美；</p>
</li>
<li><p>根据官方文档，HAProxy可以跑满10Gbps-New benchmark of HAProxy at 10 Gbps using Myricom’s 10GbE NICs (Myri-10G PCI-Express)，这个作为软件级负载均衡，也是比较惊人的；</p>
</li>
<li><p>HAProxy可以作为MySQL、邮件或其它的非web的负载均衡，我们常用于它作为MySQL(读)负载均衡；</p>
</li>
<li><p>自带强大的监控服务器状态的页面，实际环境中我们结合Nagios进行邮件或短信报警，这个也是我非常喜欢它的原因之一；</p>
</li>
<li><p>HAProxy支持虚拟主机。</p>
</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在做反向代理服务器的负载均衡时，我们通常会使用nginx的均衡配置。其实，haproxy的负载均衡也是属于这一类的。那么关于这方面的配置过程我们现在来进行一下讲解。首先，对haproxy进行一个简单的介绍，之后就是安装和配置环节了。</p>
<h2 id="HAProxy介绍"><a href="#HAProxy介绍" class="headerlink" title="HAProxy介绍"></a>HAProxy介绍</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;反向代理服务器,支持双机热备支持虚拟主机,但其配置简单,拥有非常不错的服务器健康检查功能,当其代理的后端服务器出现故障, HAProxy会自动将该服务器摘除,故障恢复后再自动将该服务器加入｡新的1.3引入了frontend,backend；frontend根据任意 HTTP请求头内容做规则匹配,然后把请求定向到相关的backend.</p>
<p><a href="http://blog.liuts.com/post/223/" target="_blank" rel="external">http://blog.liuts.com/post/223/</a> （搭建四层负载均衡器）</p>
<h2 id="keepalived简介"><a href="#keepalived简介" class="headerlink" title="keepalived简介　　"></a>keepalived简介　　</h2><p><a href="http://www.keepalived.org" target="_blank" rel="external">http://www.keepalived.org</a></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;keepalived是一个类似于layer3, 4 &amp; 5交换机制的软件，也就是我们平时说的第3层、第4层和第5层交换。Keepalived的作用是检测web服务器的状态，如果有一台web服务器死机，或工作出现故障，Keepalived将检测到，并将有故障的web服务器从系统中剔除，当web服务器工作正常后Keepalived自动将web服务器加入到服务器群中，这些工作全部自动完成，不需要人工干涉，需要人工做的只是修复故障的web服务器。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;类似的HA工具还有heatbeat、drbd等，heatbeat、drbd配置都较为复杂。</p>
<h2 id="keepalived理论工作原理"><a href="#keepalived理论工作原理" class="headerlink" title="keepalived理论工作原理"></a>keepalived理论工作原理</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;keepalived可提供vrrp以及health-check功能，可以只用它提供双机浮动的vip（vrrp虚拟路由功能），这样可以简单实现一个双机热备高可用功能。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;keepalived是一个类似于layer3, 4 &amp; 5交换机制的软件，也就是我们平时说的第3层、第4层和第5层交换。Keepalived的作用是检测web 服务器的状态。 Layer3,4&amp;5工作在IP/TCP协议栈的IP层，TCP层，及应用层,原理分别如下：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Layer3：Keepalived使用Layer3的方式工作式时，Keepalived会定期向服务器群中的服务器</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;发送一个ICMP的数据包（既我们平时用的Ping程序）,如果发现某台服务的IP地址没有激活，Keepalived便报告这台服务器失效，并将它从服务器群中剔除，这种情况的典型例子是某台服务器被非法关机。Layer3的方式是以服务器的IP地址是否有效作为服务器工作正常与否的标准。在本文中将采用这种方式。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Layer4:如果您理解了Layer3的方式，Layer4就容易了。Layer4主要以TCP端口的状态来决定服务器工作正常与否。如web server的服务端口一般是80，如果Keepalived检测到80端口没有启动，则Keepalived将把这台服务器从服务器群中剔除。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Layer5：Layer5就是工作在具体的应用层了，比Layer3,Layer4要复杂一点，在网络上占用的带宽也要大一些。Keepalived将根据用户的设定检查服务器程序的运行是否正常，如果与用户的设定不相符，则Keepalived将把服务器从服务器群中剔除。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;vip即虚拟ip，是附在主机网卡上的，即对主机网卡进行虚拟，此IP仍然是占用了此网段的某个IP。</p>
<h2 id="keepalived作用"><a href="#keepalived作用" class="headerlink" title="keepalived作用"></a>keepalived作用</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;随着你的网站业务量的增长你网站的服务器压力越来越大？需要负载均衡方案！商业的硬件如F5又太贵，你们又是创业型互联公司如何有效节约成本，节省不必要的浪费？同时实现商业硬件一样的高性能高可用的功能？有什么好的负载均衡可伸张可扩展的方案吗？答案是肯定的！有！我们利用 LVS+Keepalived基于完整开源软件的架构可以为你提供一个负载均衡及高可用的服务器。</p>
<h3 id="LVS-Keepalived-介绍"><a href="#LVS-Keepalived-介绍" class="headerlink" title="LVS+Keepalived 介绍"></a>LVS+Keepalived 介绍</h3><h4 id="LVS"><a href="#LVS" class="headerlink" title="LVS"></a>LVS</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;LVS是Linux Virtual Server的简写，意即Linux虚拟服务器，是一个虚拟的服务器集群系统。本项目在1998年5月由章文嵩博士成立，是中国国内最早出现的自由软件项目之一.目前有三种IP负载均衡技术（VS/NAT、VS/TUN和VS/DR）八种调度算法（rr,wrr,lc,wlc,lblc,lblcr,dh,sh）。</p>
<h4 id="Keepalvied"><a href="#Keepalvied" class="headerlink" title="Keepalvied"></a>Keepalvied</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Keepalived在这里主要用作RealServer的健康状态检查以及LoadBalance主机和BackUP主机之间failover的实现。keepalived简介　　keepalived是一个类似于layer3, 4 &amp; 5交换机制的软件，也就是我们平时说的第3层、第4层和第5层交换。Keepalived的作用是检测web服务器的状态，如果有一台web服务器死机，或工作出现故障，Keepalived将检测到，并将有故障的web服务器从系统中剔除，当web服务器工作正常后Keepalived自动将web服务器加入到服务器群中，这些工作全部自动完成，不需要人工干涉，需要人工做的只是修复故障的web服务器。</p>
<h2 id="Keepalived介绍"><a href="#Keepalived介绍" class="headerlink" title="Keepalived介绍"></a>Keepalived介绍</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Keepalived是一个基于VRRP协议来实现的WEB 服务高可用方案，可以利用其来避免单点故障。一个WEB服务至少会有2台服务器运行Keepalived，一台为主服务器（MASTER），一台为备份服务器（BACKUP），但是对外表现为一个虚拟IP，主服务器会发送特定的消息给备份服务器，当备份服务器收不到这个消息的时候，即主服务器宕机的时候，备份服务器就会接管虚拟IP，继续提供服务，从而保证了高可用性。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">+-------------VIP(192.168.0.7)------------------+</div><div class="line">|                         |                      |</div><div class="line">|                         |                      |</div><div class="line">server(MASTER) &lt;----keepalived----&gt; server(BACKUP)</div><div class="line">(192.168.0.1)                        (192.168.0.2)</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;keepalived是VRRP的完美实现，因此在介绍keepalived之前，先介绍一下VRRP的原理。</p>
<h2 id="VRRP协议简介"><a href="#VRRP协议简介" class="headerlink" title="VRRP协议简介"></a>VRRP协议简介</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在现实的网络环境中，两台需要通信的主机大多数情况下并没有直接的物理连接。对于这样的情况，它们之间路由怎样选择？主机如何选定到达目的主机的下一跳路由，这个问题通常的解决方法有二种：</p>
<ul>
<li>在主机上使用动态路由协议(RIP、OSPF等)</li>
<li>在主机上配置静态路由</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;很明显，在主机上配置路态路由是非常不切实际的，因为管理、维护成本以及是否支持等诸多问题。配置静态路由就变得十分流行，但路由器(或者说默认网关default gateway)却经常成为单点。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;VRRP的目的就是为了解决静态路由单点故障问题。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;VRRP通过一竞选(election)协议来动态的将路由任务交给LAN中虚拟路由器中的某台VRRP路由器。</p>
<h3 id="工作机制"><a href="#工作机制" class="headerlink" title="工作机制"></a>工作机制</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在一个VRRP虚拟路由器中，有多台物理的VRRP路由器，但是这多台的物理的机器并不能同时工作，而是由一台称为MASTER的负责路由工作，其它的都是BACKUP，MASTER并非一成不变，VRRP让每个VRRP路由器参与竞选，最终获胜的就是MASTER。MASTER拥有一些特权，比如 拥有虚拟路由器的IP地址，我们的主机就是用这个IP地址作为静态路由的。拥有特权的MASTER要负责转发发送给网关地址的包和响应ARP请求。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;VRRP通过竞选协议来实现虚拟路由器的功能，所有的协议报文都是通过IP多播(multicast)包(多播地址 224.0.0.18)形式发送的。虚拟路由器由VRID(范围0-255)和一组IP地址组成，对外表现为一个周知的MAC地址。所以，在一个虚拟路由 器中，不管谁是MASTER，对外都是相同的MAC和IP(称之为VIP)。客户端主机并不需要因为MASTER的改变而修改自己的路由配置，对他们来 说，这种主从的切换是透明的。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在一个虚拟路由器中，只有作为MASTER的VRRP路由器会一直发送VRRP广告包(VRRPAdvertisement message)，BACKUP不会抢占MASTER，除非它的优先级(priority)更高。当MASTER不可用时(BACKUP收不到广告包)， 多台BACKUP中优先级最高的这台会被抢占为MASTER。这种抢占是非常快速的（小于1s），以保证服务的连续性。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;由于安全性考虑，VRRP包使用了加密协议进行加密。</p>
<h3 id="vrrp简介"><a href="#vrrp简介" class="headerlink" title="vrrp简介"></a>vrrp简介</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;随着Internet的迅猛发展，基于网络的应用逐渐增多。这就对网络的可靠性提出了越来越高的要求。斥资对所有网络设备进行更新当然是一种很好的可靠性解决方案；但本着保护现有投资的角度考虑，可以采用廉价冗余的思路，在可靠性和经济性方面找到平衡点。 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;虚拟路由冗余协议就是一种很好的解决方案。在该协议中，对共享多存取访问介质（如以太网）上终端IP设备的默认网关(Default Gateway)进行冗余备份，从而在其中一台路由设备宕机时，备份路由设备及时接管转发工作，向用户提供透明的切换，提高了网络服务质量。  </p>
<h4 id="一、协议概述"><a href="#一、协议概述" class="headerlink" title="一、协议概述"></a>一、协议概述</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在基于TCP/IP协议的网络中，为了保证不直接物理连接的设备之间的通信，必须指定路由。目前常用的指定路由的方法有两种：一种是通过路由协议（比如：内部路由协议RIP和OSPF）动态学习；另一种是静态配置。在每一个终端都运行动态路由协议是不现实的，大多客户端操作系统平台都不支持动态路由协议，即使支持也受到管理开销、收敛度、安全性等许多问题的限制。因此普遍采用对终端IP设备静态路由配置，一般是给终端设备指定一个或者多个默认网关(Default Gateway)。静态路由的方法简化了网络管理的复杂度和减轻了终端设备的通信开销，但是它仍然有一个缺点：如果作为默认网关的路由器损坏，所有使用该网关为下一跳主机的通信必然要中断。即便配置了多个默认网关，如不重新启动终端设备，也不能切换到新的网关。采用虚拟路由冗余协议 (Virtual Router Redundancy Protocol，简称VRRP)可以很好的避免静态指定网关的缺陷。 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在VRRP协议中，有两组重要的概念：VRRP路由器和虚拟路由器，主控路由器和备份路由器。VRRP路由器是指运行VRRP的路由器，是物理实体，虚拟路由器是指VRRP协议创建的，是逻辑概念。一组VRRP路由器协同工作，共同构成一台虚拟路由器。该虚拟路由器对外表现为一个具有唯一固定IP地址和MAC地址的逻辑路由器。处于同一个VRRP组中的路由器具有两种互斥的角色：主控路由器和备份路由器，一个VRRP组中有且只有一台处于主控角色的路由器，可以有一个或者多个处于备份角色的路由器。VRRP协议使用选择策略从路由器组中选出一台作为主控，负责ARP相应和转发IP数据包，组中的其它路由器作为备份的角色处于待命状态。当由于某种原因主控路由器发生故障时，备份路由器能在几秒钟的时延后升级为主路由器。由于此切换非常迅速而且不用改变IP地址和MAC地址，故对终端使用者系统是透明的。  </p>
<h4 id="二、工作原理"><a href="#二、工作原理" class="headerlink" title="二、工作原理"></a>二、工作原理</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;一个VRRP路由器有唯一的标识：VRID，范围为0—255。该路由器对外表现为唯一的虚拟MAC地址，地址的格式为00-00-5E-00-01-[VRID]。主控路由器负责对ARP请求用该MAC地址做应答。这样，无论如何切换，保证给终端设备的是唯一一致的IP和MAC地址，减少了切换对终端设备的影响。 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;VRRP控制报文只有一种：VRRP通告(advertisement)。它使用IP多播数据包进行封装，组地址为224.0.0.18，发布范围只限于同一局域网内。这保证了VRID在不同网络中可以重复使用。为了减少网络带宽消耗只有主控路由器才可以周期性的发送VRRP通告报文。备份路由器在连续三个通告间隔内收不到VRRP或收到优先级为0的通告后启动新的一轮VRRP选举。 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在VRRP路由器组中，按优先级选举主控路由器，VRRP协议中优先级范围是0—255。若VRRP路由器的IP地址和虚拟路由器的接口IP地址相同，则称该虚拟路由器作VRRP组中的IP地址所有者；IP地址所有者自动具有最高优先级：255。优先级0一般用在IP地址所有者主动放弃主控者角色时使用。可配置的优先级范围为1—254。优先级的配置原则可以依据链路的速度和成本、路由器性能和可靠性以及其它管理策略设定。主控路由器的选举中，高优先级的虚拟路由器获胜，因此，如果在VRRP组中有IP地址所有者，则它总是作为主控路由的角色出现。对于相同优先级的候选路由器，按照IP地址大小顺序选举。VRRP还提供了优先级抢占策略，如果配置了该策略，高优先级的备份路由器便会剥夺当前低优先级的主控路由器而成为新的主控路由器。 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;为了保证VRRP协议的安全性，提供了两种安全认证措施：明文认证和IP头认证。明文认证方式要求：在加入一个VRRP路由器组时，必须同时提供相同的VRID和明文密码。适合于避免在局域网内的配置错误，但不能防止通过网络监听方式获得密码。IP头认证的方式提供了更高的安全性，能够防止报文重放和修改等攻击。 </p>
<h4 id="三、-应用实例"><a href="#三、-应用实例" class="headerlink" title="三、 应用实例"></a>三、 应用实例</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;最典型的VRRP应用：RTA、RTB组成一个VRRP路由器组，假设RTB的处理能力高于RTA，则将RTB配置成IP地址所有者，H1、H2、H3的默认网关设定为RTB。则RTB成为主控路由器，负责ICMP重定向、ARP应答和IP报文的转发；一旦RTB失败，RTA立即启动切换，成为主控，从而保证了对客户透明的安全切换。 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在VRRP应用中，RTA在线时RTB只是作为后备，不参与转发工作，闲置了路由器RTA和链路L1。通过合理的网络设计，可以到达备份和负载分担双重效果。让RTA、RTB同时属于互为备份的两个VRRP组：在组1中RTA为IP地址所有者；组2中RTB为IP地址所有者。将H1的默认网关设定为RTA；H2、H3的默认网关设定为RTB。这样，既分担了设备负载和网络流量，又提高了网络可靠性。 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;VRRP协议的工作机理与CISCO公司的HSRP（Hot Standby Routing Protocol）有许多相似之处。但二者主要的区别是在CISCO的HSRP中，需要单独配置一个IP地址作为虚拟路由器对外体现的地址，这个地址不能是组中任何一个成员的接口地址。 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用VRRP协议，不用改造目前的网络结构，最大限度保护了当前投资，只需最少的管理费用，却大大提升了网络性能，具有重大的应用价值。</p>
<h2 id="keepalive的简单应用——管理VIP的飘动"><a href="#keepalive的简单应用——管理VIP的飘动" class="headerlink" title="keepalive的简单应用——管理VIP的飘动"></a>keepalive的简单应用——管理VIP的飘动</h2><p>from:<a href="http://www.cnblogs.com/killkill/archive/2010/12/31/1922360.html" target="_blank" rel="external">http://www.cnblogs.com/killkill/archive/2010/12/31/1922360.html</a></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;VIP的飘动可以为我们解决很多问题，以前我试过使用ifup/ifdown的方式控制网卡的up/down来实现，这种方式有个小问题，就是每次VIP 飘动之后都要等上几十秒才能生效，感觉时间比较长，而且还要配合一些逻辑脚本才能很好地工作，有没有更好的方法呢？当然有，这就是本文的主角—— keepalived。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;安装很简单：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">tar zxvf keepalived-1.1.20.tar.gz</div><div class="line"><span class="built_in">cd</span> keepalived-1.1.20</div><div class="line">./configure --prefix=/</div><div class="line">make</div><div class="line">make install</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改一下 /etc/keepalived/keepalived.conf 这个配置文件就可以用了，以下是我的环境，192.168.10.141和192.168.10.142是两个VIP，可以在两台服务器之间飘动：</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/haproxy+keepalived%E5%AE%9E%E7%8E%B0%E9%AB%98%E5%8F%AF%E7%94%A8%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1/01.png?raw=true" alt=""> </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主机的配置：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div></pre></td><td class="code"><pre><div class="line">global_defs &#123;</div><div class="line">    notification_email &#123;</div><div class="line">        failover@firewall.loc</div><div class="line">    &#125;</div><div class="line">    notification_email_from Alexandre.Cassen@firewall.loc</div><div class="line">    smtp_server 192.168.0.48</div><div class="line">    smtp_connect_timeout 10</div><div class="line">    router_id nginx</div><div class="line">&#125;</div><div class="line">vrrp_instance VI_141 &#123;</div><div class="line">    state BACKUP</div><div class="line">    interface eth0</div><div class="line">    virtual_router_id 141</div><div class="line">    priority 50</div><div class="line">    advert_int 1</div><div class="line">    authentication &#123;</div><div class="line">        auth_type PASS</div><div class="line">        auth_pass 141</div><div class="line">    &#125;</div><div class="line">    virtual_ipaddress &#123;</div><div class="line">        192.168.10.141/26 dev eth0</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">vrrp_instance VI_142 &#123;</div><div class="line">    state BACKUP</div><div class="line">    interface eth0</div><div class="line">    virtual_router_id 142</div><div class="line">    priority 100</div><div class="line">    advert_int 1</div><div class="line">    authentication &#123;</div><div class="line">        auth_type PASS</div><div class="line">        auth_pass 142</div><div class="line">    &#125;</div><div class="line">    virtual_ipaddress &#123;</div><div class="line">        192.168.10.142/26 dev eth0</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;备机的配置：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div></pre></td><td class="code"><pre><div class="line">global_defs &#123;</div><div class="line">    notification_email &#123;</div><div class="line">        failover@firewall.loc</div><div class="line">    &#125;</div><div class="line">    notification_email_from Alexandre.Cassen@firewall.loc</div><div class="line">    smtp_server 10.168.0.48</div><div class="line">    smtp_connect_timeout 10</div><div class="line">    router_id nginx</div><div class="line">&#125;</div><div class="line"></div><div class="line">vrrp_instance VI_141 &#123;</div><div class="line">    state BACKUP</div><div class="line">    interface eth0</div><div class="line">    virtual_router_id 141</div><div class="line">    priority 100</div><div class="line">    advert_int 1</div><div class="line">    authentication &#123;</div><div class="line">    auth_type PASS</div><div class="line">    auth_pass 141</div><div class="line">    &#125;</div><div class="line">    virtual_ipaddress &#123;</div><div class="line">        192.168.10.141/26 dev eth0</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">vrrp_instance VI_142 &#123;</div><div class="line">    state BACKUP</div><div class="line">    interface eth0</div><div class="line">    virtual_router_id 142</div><div class="line">    priority 50</div><div class="line">    advert_int 1</div><div class="line">    authentication &#123;</div><div class="line">        auth_type PASS</div><div class="line">        auth_pass 142</div><div class="line">    &#125;</div><div class="line">    virtual_ipaddress &#123;</div><div class="line">        192.168.10.142/26 dev eth0</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;乍一看，主机和备机的配置文件是一样的，仔细看一下priority的值，使用以下命令即可将keepalived加入linux的服务中：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">chkconfig --add keepalived ;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;通过启、停keepalived这个服务即可观察到VIP的飘动，至于为什么VIP飘动后可以很快地生效，还有待研究。</p>
<h2 id="haproxy-keepalived实现高可用负载均衡"><a href="#haproxy-keepalived实现高可用负载均衡" class="headerlink" title="haproxy+keepalived实现高可用负载均衡"></a>haproxy+keepalived实现高可用负载均衡</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;环境：</p>
<ul>
<li>haproxy keepalived 主：192.168.1.192</li>
<li>haproxy keepalived 备：192.168.1.193</li>
<li>vip：192.168.1.200</li>
<li>web：192.168.1.187:80 192.168.1.187:8000</li>
</ul>
<p><img src="https://github.com/hcldirgit/image/blob/master/haproxy+keepalived%E5%AE%9E%E7%8E%B0%E9%AB%98%E5%8F%AF%E7%94%A8%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1/02.jpeg?raw=true" alt=""></p>
<h3 id="一：安装过程，在192-168-1-192上："><a href="#一：安装过程，在192-168-1-192上：" class="headerlink" title="一：安装过程，在192.168.1.192上："></a>一：安装过程，在192.168.1.192上：</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;keepalived的安装:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">tar -zxvf keepalived-1.1.17.tar.gz</div><div class="line"></div><div class="line">ln -s /usr/src/kernels/2.6.18-128.el5-i686/ /usr/src/linux</div><div class="line"></div><div class="line"><span class="built_in">cd</span> keepalived-1.1.17</div><div class="line"></div><div class="line">./configure --prefix=/ --mandir=/usr/<span class="built_in">local</span>/share/man/ --with-kernel-dir=/usr/src/kernels/2.6.18-128.el5-i686/</div><div class="line"></div><div class="line">make &amp;&amp; make install</div><div class="line"></div><div class="line"><span class="built_in">cd</span> /etc/keepalived/</div><div class="line"></div><div class="line">mv keepalived.conf keepalived.conf.default</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div></pre></td><td class="code"><pre><div class="line">vi keepalived.conf</div><div class="line"></div><div class="line"></div><div class="line">! Configuration File <span class="keyword">for</span> keepalived</div><div class="line"></div><div class="line">vrrp_script chk_http_port &#123;</div><div class="line">    script <span class="string">"/etc/keepalived/check_haproxy.sh"</span></div><div class="line">    interval 2</div><div class="line">    weight 2</div><div class="line"></div><div class="line">    global_defs &#123;</div><div class="line">        router_id LVS_DEVEL</div><div class="line">    &#125;</div><div class="line">    vrrp_instance VI_1 &#123;</div><div class="line">        state MASTER <span class="comment">#192.168.1.193上改为BACKUP</span></div><div class="line">        interface eth0</div><div class="line">        virtual_router_id 51 </div><div class="line">        priority 150 <span class="comment">#192.168.1.193上改为120</span></div><div class="line">        advert_int 1</div><div class="line">        authentication &#123;</div><div class="line">            auth_type PASS</div><div class="line">            auth_pass 1111</div><div class="line">        &#125;</div><div class="line"></div><div class="line">        track_script &#123;</div><div class="line">            chk_http_port</div><div class="line">        &#125;</div><div class="line"></div><div class="line">        virtual_ipaddress &#123;</div><div class="line">            192.168.1.200 </div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">vi /etc/keepalived/check_haproxy.sh</div><div class="line"></div><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">A=`ps -C haproxy --no-header |wc -l`</div><div class="line"><span class="keyword">if</span> [ <span class="variable">$A</span> -eq 0 ];<span class="keyword">then</span></div><div class="line">    /usr/<span class="built_in">local</span>/haproxy/sbin/haproxy -f /usr/<span class="built_in">local</span>/haproxy/conf/haproxy.cfg</div><div class="line">    sleep 3</div><div class="line">    <span class="keyword">if</span> [ `ps -C haproxy --no-header |wc -l` -eq 0 ];<span class="keyword">then</span></div><div class="line">        /etc/init.d/keepalived stop</div><div class="line">    <span class="keyword">fi</span></div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">chmod 755 /etc/keepalived/check_haproxy.sh</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;haproxy的安装(主备都一样)：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">tar -zxvf haproxy-1.4.9.tar.gz</div><div class="line"><span class="built_in">cd</span> haproxy-1.4.9</div><div class="line">make TARGET=linux26 PREFIX=/usr/<span class="built_in">local</span>/haproxy install</div><div class="line"><span class="built_in">cd</span> /usr/<span class="built_in">local</span>/haproxy/</div><div class="line">mkdir conf logs</div><div class="line"><span class="built_in">cd</span> conf</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div></pre></td><td class="code"><pre><div class="line">vi haproxy.cfg</div><div class="line"></div><div class="line">global</div><div class="line"><span class="built_in">log</span> 127.0.0.1 local3 info</div><div class="line">maxconn 4096</div><div class="line">user nobody</div><div class="line">group nobody</div><div class="line">daemon</div><div class="line">nbproc 1</div><div class="line">pidfile /usr/<span class="built_in">local</span>/haproxy/logs/haproxy.pid</div><div class="line"></div><div class="line">defaults</div><div class="line">maxconn 2000</div><div class="line">contimeout 5000</div><div class="line">clitimeout 30000</div><div class="line">srvtimeout 30000</div><div class="line">mode http</div><div class="line"><span class="built_in">log</span> global</div><div class="line"><span class="built_in">log</span> 127.0.0.1 local3 info</div><div class="line">stats uri /admin?stats</div><div class="line">option forwardfor</div><div class="line"></div><div class="line">frontend http_server</div><div class="line"><span class="built_in">bind</span> :80</div><div class="line"><span class="built_in">log</span> global</div><div class="line">default_backend info_cache</div><div class="line">acl <span class="built_in">test</span> hdr_dom(host) -i test.domain.com</div><div class="line">use_backend cache_test <span class="keyword">if</span> <span class="built_in">test</span></div><div class="line"></div><div class="line">backend info_cache</div><div class="line"><span class="comment">#balance roundrobin</span></div><div class="line">balance <span class="built_in">source</span></div><div class="line">option httpchk HEAD /haproxy.txt HTTP/1.1\r\nHost:192.168.1.187</div><div class="line">server inst2 192.168.1.187:80 check inter 5000 fall 3</div><div class="line"></div><div class="line"></div><div class="line">backend cache_test</div><div class="line">balance roundrobin</div><div class="line"><span class="comment">#balance source</span></div><div class="line">option httpchk HEAD /haproxy.txt HTTP/1.1\r\nHost:test.domain.com</div><div class="line">server inst1 192.168.1.187:8000 check inter 5000 fall 3</div></pre></td></tr></table></figure>
<h3 id="二：再两台机器上都分别启动："><a href="#二：再两台机器上都分别启动：" class="headerlink" title="二：再两台机器上都分别启动："></a>二：再两台机器上都分别启动：</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">/etc/init.d/keepalived start <span class="comment">#这条命令会自动把haproxy启动</span></div></pre></td></tr></table></figure>
<h3 id="三：测试："><a href="#三：测试：" class="headerlink" title="三：测试："></a>三：测试：</h3><ol>
<li>再两台机器上分别执行ip add</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">eth0: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc pfifo_fast qlen 1000</div><div class="line">link/ether 00:0c:29:98:<span class="built_in">cd</span>:c0 brd ff:ff:ff:ff:ff:ff</div><div class="line">inet 192.168.1.192/24 brd 192.168.1.255 scope global eth0</div><div class="line">inet 192.168.1.200/32 scope global eth0</div><div class="line">inet6 fe80::20c:29ff:fe98:cdc0/64 scope link</div><div class="line">valid_lft forever preferred_lft forever</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;备: </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">eth0: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc pfifo_fast qlen 1000</div><div class="line">link/ether 00:0c:29:a6:0c:7e brd ff:ff:ff:ff:ff:ff</div><div class="line">inet 192.168.1.193/24 brd 255.255.255.254 scope global eth0</div><div class="line">inet6 fe80::20c:29ff:fea6:c7e/64 scope link</div><div class="line">valid_lft forever preferred_lft forever</div></pre></td></tr></table></figure>
<ol>
<li><p>停掉主上的haproxy，3秒后keepalived会自动将其再次启动</p>
</li>
<li><p>停掉主的keepalived，备机马上接管服务</p>
</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;备: </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">eth0: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc pfifo_fast qlen 1000</div><div class="line">link/ether 00:0c:29:a6:0c:7e brd ff:ff:ff:ff:ff:ff</div><div class="line">inet 192.168.1.193/24 brd 255.255.255.254 scope global eth0</div><div class="line">inet 192.168.1.200/32 scope global eth0</div><div class="line">inet6 fe80::20c:29ff:fea6:c7e/64 scope link</div><div class="line">valid_lft forever preferred_lft forever</div></pre></td></tr></table></figure>
<ol>
<li>更改hosts</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">192.168.1.200 test.com</div><div class="line">192.168.1.200 test.domain.com</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;通过IE测试，可以发现</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;test.com的请求发向了192.168.1.187:80</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;test.domain.com的请求发向了192.168.1.187:8000</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/haproxy+keepalived%E5%AE%9E%E7%8E%B0%E9%AB%98%E5%8F%AF%E7%94%A8%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1/03.jpeg?raw=true" alt=""></p>
<p><img src="https://github.com/hcldirgit/image/blob/master/haproxy+keepalived%E5%AE%9E%E7%8E%B0%E9%AB%98%E5%8F%AF%E7%94%A8%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1/04.jpeg?raw=true" alt=""></p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/LVS/" rel="tag"># LVS</a>
          
            <a href="/tags/keepalived/" rel="tag"># keepalived</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/09/03/LVS/21. Keepalived配置及典型应用案例/" rel="next" title="Keepalived配置及典型应用案例">
                <i class="fa fa-chevron-left"></i> Keepalived配置及典型应用案例
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/09/03/LVS/22. keepalived之vrrp_script总结/" rel="prev" title="keepalived之vrrp_script总结">
                keepalived之vrrp_script总结 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/0.png"
               alt="失落的乐章" />
          <p class="site-author-name" itemprop="name">失落的乐章</p>
           
              <p class="site-description motion-element" itemprop="description">失落的乐章的Blog</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/">
                <span class="site-state-item-count">627</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">38</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/hcldirgit" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#HAProxy介绍"><span class="nav-number">1.</span> <span class="nav-text">HAProxy介绍</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#keepalived简介"><span class="nav-number">2.</span> <span class="nav-text">keepalived简介　　</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#keepalived理论工作原理"><span class="nav-number">3.</span> <span class="nav-text">keepalived理论工作原理</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#keepalived作用"><span class="nav-number">4.</span> <span class="nav-text">keepalived作用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#LVS-Keepalived-介绍"><span class="nav-number">4.1.</span> <span class="nav-text">LVS+Keepalived 介绍</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#LVS"><span class="nav-number">4.1.1.</span> <span class="nav-text">LVS</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Keepalvied"><span class="nav-number">4.1.2.</span> <span class="nav-text">Keepalvied</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Keepalived介绍"><span class="nav-number">5.</span> <span class="nav-text">Keepalived介绍</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#VRRP协议简介"><span class="nav-number">6.</span> <span class="nav-text">VRRP协议简介</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#工作机制"><span class="nav-number">6.1.</span> <span class="nav-text">工作机制</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#vrrp简介"><span class="nav-number">6.2.</span> <span class="nav-text">vrrp简介</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#一、协议概述"><span class="nav-number">6.2.1.</span> <span class="nav-text">一、协议概述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#二、工作原理"><span class="nav-number">6.2.2.</span> <span class="nav-text">二、工作原理</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#三、-应用实例"><span class="nav-number">6.2.3.</span> <span class="nav-text">三、 应用实例</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#keepalive的简单应用——管理VIP的飘动"><span class="nav-number">7.</span> <span class="nav-text">keepalive的简单应用——管理VIP的飘动</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#haproxy-keepalived实现高可用负载均衡"><span class="nav-number">8.</span> <span class="nav-text">haproxy+keepalived实现高可用负载均衡</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#一：安装过程，在192-168-1-192上："><span class="nav-number">8.1.</span> <span class="nav-text">一：安装过程，在192.168.1.192上：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#二：再两台机器上都分别启动："><span class="nav-number">8.2.</span> <span class="nav-text">二：再两台机器上都分别启动：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#三：测试："><span class="nav-number">8.3.</span> <span class="nav-text">三：测试：</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">失落的乐章</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.1"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.1"></script>



  


  




	





  





  





  






  





  

  

  

  

  

  

</body>
</html>
